package com.wy.util.Http;

import java.io.IOException;


import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;


/**
 * 网页抓取
 * @author WY
 *
 */
@SuppressWarnings("deprecation")
public final class HttpClientUtil {
	
	/**
	 * 抓取网页内容
	 * @param url 网页连接
	 * @return 返回网页HTML
	 */
	public static String getHtmlByURL(String url,String encode){
		
		HttpClient httpClient = new DefaultHttpClient();
		
		//设置连接时间为15秒
		httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 15000);
		//设置读取超时15秒
		httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 15000);
		
		HttpGet get = new HttpGet(url);
		HttpResponse response = null;
		try {
			response = httpClient.execute(get);
			//状态码
			Integer statusCode = response.getStatusLine().getStatusCode();
			if(statusCode == HttpStatus.SC_OK){
				return EntityUtils.toString(response.getEntity(),encode);
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			httpClient.getConnectionManager().shutdown();
		}
		return null;
	}
	
	
	/**
	 * 解析HTML，得到Elements对象
	 * @param html	内容
	 * @param query  选择器条件
	 * @return
	 */
	public static Elements parseHtmlToElements(String html, String query){
		Document doc = Jsoup.parse(html);
		Elements elements = null;
		elements = doc.select(query);	//得到章节链接标签元素
		return elements;
	}
	
	/**
	 * 将Elements对象解析成List<Chapter>集合
	 * @param elements	章节标签元素集合
	 * @return List<Chapter>
	 */
//	public static List<Chapter> parseElementsToCharpterList(Elements elements){
//		String targetUrl = null;
//		String title = null;
//		List<Chapter> list = new ArrayList<Chapter>();
//		
//		if(!elements.isEmpty() && elements.size() >0){
//			for(int i=0;i<elements.size();i++){
//				Chapter c = new Chapter();
//				targetUrl = elements.get(i).attr("href");
//				if(targetUrl != null && !targetUrl.trim().equals("")){
//					title = elements.get(i).text();
//					c.setId(i);
//					c.setTitle(title);
//					c.setTargetUrl(targetUrl);
//					list.add(c);
//				}
//			}
//		}
//		return list;
//	}
	
	/**
	 * 解析小说目录链接，得到List<Chapter>集合
	 * @param catalogUrl 小说目录页面URL
	 * @param webName 网站名称；如：www.59to.com
	 * @return List<Chapter>
	 */
//	public static List<Chapter> getChapterListByCatalogUrl(String catalogUrl,String encode, String query){
//		String html = getHtmlByURL(catalogUrl,encode);
//		return parseElementsToCharpterList(parseHtmlToElements(html, query));
//	}
//	
//	public static String getChapterContentByUrl(String chapterUrl){
//		
//		return null;
//	}
	
	/**
	 * 已过时！！！
	 * 解析书籍目录页面
	 * @param html 传入目录代码HTML
	 * @param webName 网站域名，如：www.59to.com/www.59to.org
	 * @return Book
	 */
//	public static Book parseHtml(String html,String webName){
//		Book book = new Book();
//		String link = null;
//		String title= null;
//		
//		Document doc = Jsoup.parse(html);
//		if(webName.equals(Constant.WEB_NAME_WUJIUWENXUE_COM)){				//www.59to.com
//			
//			Elements linkElements = doc.select(Constant.WEB_NAME_WUJIUWENXUE_COM_SELECT);
//			if(!linkElements.isEmpty() && linkElements.size() > 0){
//				
//				List<Chapter> csList = new ArrayList<Chapter>();
//				
//				Element element = null;
//				for(int i=0;i<linkElements.size();i++){
//					Chapter c = new Chapter();
//					element = linkElements.get(i);
//					link = element.attr("href");
//					if(link != null){
//						title = element.text();
//						c.setId(i);
//						c.setTitle(title);
//						c.setTargetUrl(link);
//						csList.add(c);
//					}
//				}
//				book.setChapterList(csList);
////				for(Chapter p : csList){
////					System.out.println(p.getId() +"--->"+ p.getTitle());
////				}
//				
//			}
//		}
//		return book;
//	}
}
